import allure
import logging
import pytest
from base.base_analyze import analyze_data
from base.base_driver import BaseDriver
from data.constant import *
from page.page_0 import PageTo
import logging
allure.testcase(URL_HJX)
_driver=None
"""
@pytest.fixture()
def login(request,user_name="",user_passwd=""):
    # 实例化驱动 role:"ZPT","YX","FX","XXZX" 总平台，院校，分校，学习中心,学员端
    role = request.param  # 通过 request.param 获取测试参数
    global _driver
    _driver = BaseDriver.get_driver()
    BaseDriver.set_switch(True)
    if role =="XYD":
        # 打开网址
        _driver.get(URL_XYD)
        page = PageTo(_driver)
        i = 4
        if user_name=="":
            user_name = analyze_data("data_2_role_login.json")[i]["username"]
            user_passwd = analyze_data("data_2_role_login.json")[i]["password"]
            logging.info(user_name)
        page.pageEnterStudent.clickLoginByStudyNumber() #切换学号登录
        page.pageEnterStudent.input_st_username(user_name) # 输入账号
        page.pageEnterStudent.input_st_passwd(user_passwd) # 输入密码
        page.pageEnterStudent.click_login_btn() # 点击登录
    else:
        if role == "ZPT":i=0 #使用总平台用户登录
        elif role =="YX":i=1 #使用院校用户登录
        elif role == "FX":i = 2 #使用分校用户登录
        else:
            i = 3 #使用学习中心用户登录
        # 打开网址
        _driver.get(URL)
        # logging.info("role"+str(role))
        page = PageTo(_driver)
        user_name = analyze_data("data_2_role_login.json")[i]["username"]
        user_passwd = analyze_data("data_2_role_login.json")[i]["password"]
        page.loginPage.input_username(user_name) # 输入账号
        page.loginPage.input_passwd(user_passwd) # 输入密码
        page.loginPage.click_login_btn() # 点击登录
    yield page
    BaseDriver.set_switch(False)
    BaseDriver.quit_driver()






@pytest.fixture()
def login_anyone(request,user_name="",user_passwd=""):
# def login_anyone(request):
    # 实例化驱动 role:"ZPT","YX","FX","XXZX" 总平台，院校，分校，学习中心
    role = request.param  # 通过 request.param 获取测试参数
    logging.info(len(role))
    logging.info("role"+str(role))
    global _driver
    if role =="XYD":
        # 打开网址
        _driver.get(URL_XYD)
        page = PageTo(_driver)
        i = 4
        if user_name=="":
            user_name = analyze_data("data_2_role_login.json")[i]["username"]
            user_passwd = analyze_data("data_2_role_login.json")[i]["password"]
        page.pageEnterStudent.clickLoginByStudyNumber() #切换学号登录
        page.pageEnterStudent.input_st_username(user_name) # 输入账号
        page.pageEnterStudent.input_st_passwd(user_passwd) # 输入密码
        page.pageEnterStudent.click_login_btn() # 点击登录
    else:
        if role == "ZPT":i=0 #使用总平台用户登录
        elif role =="YX":i=1 #使用院校用户登录
        elif role == "FX":i = 2 #使用分校用户登录
        else:i = 3 #使用学习中心用户登录
        _driver = BaseDriver.get_driver()
        BaseDriver.set_switch(True)
        # 打开网址
        _driver.get(URL)
        logging.info("role"+str(role))
        page = PageTo(_driver)
        user_name = analyze_data("data_2_role_login.json")[i]["username"]
        user_passwd = analyze_data("data_2_role_login.json")[i]["password"]
        page.loginPage.input_username(user_name) # 输入账号
        page.loginPage.input_passwd(user_passwd) # 输入密码
        page.loginPage.click_login_btn() # 点击登录
    yield page
    BaseDriver.set_switch(False)
    BaseDriver.quit_driver()
"""
@pytest.fixture()
def login_hjx(request,user_name="",user_passwd=""):
# def login_anyone(request):
    # 实例化驱动 role:"ZPT","YX","FX","XXZX" 总平台，院校，分校，学习中心
    # role = request.param  # 通过 request.param 获取测试参数
    logging.info('登录')
    # logging.info("role"+str())
    global _driver

    _driver = BaseDriver.get_driver()
    BaseDriver.set_switch(True)
    # 打开网址
    _driver.get(URL_HJX)
    # logging.info("role"+str())
    page = PageTo(_driver)
    user_name = analyze_data("data_3_role_login.json")[0]["username"]
    user_passwd = analyze_data("data_3_role_login.json")[0]["password"]
    page.loginPage.input_username(user_name)    # 输入账号
    page.loginPage.input_passwd(user_passwd)    # 输入密码
    page.loginPage.click_login_btn()    # 点击登录
    yield page
    BaseDriver.set_switch(False)
    BaseDriver.quit_driver()


    # if role == "XYD":
# # 打开网址
#     _driver.get(URL_HJX)
#     page = PageTo(_driver)
#     # if user_name == "":
#     #     user_name = analyze_data("data_1_login.json")["username"]
#     #     user_passwd = analyze_data("data_1_login.json")["password"]
#     #     logging.info(user_name)
#     # page.pageEnterStudent.clickLoginByStudyNumber()  # 切换学号登录
#     # page.pageEnterStudent.input_st_username(user_name)  # 输入账号
#     # page.pageEnterStudent.input_st_passwd(user_passwd)  # 输入密码
#     # page.pageEnterStudent.click_login_btn()  # 点击登录
#
    # user_name = analyze_data("data_1_login.json")["username"]
    # user_passwd = analyze_data("data_1_login.json")["password"]
    # print(user_name)
    # print(user_passwd)
    # # 输入用户名
    # page.loginPage.input_username(user_name)
    # # 输入密码
    # page.loginPage.input_passwd(user_passwd)
    # # 点击登录按钮
    # page.loginPage.click_login_btn()










@pytest.fixture()
def start():
    # 实例化驱动
    driver = BaseDriver.get_driver()
    BaseDriver.set_switch(True)
    # 打开网址
    driver.get(URL_HJX)
    page = PageTo(driver)
    # 返回页面集合
    yield page
    BaseDriver.set_switch(False)
    BaseDriver.quit_driver()



@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport():

    out = yield
    report = out.get_result()
    if report.when == "call" and report.failed:
        PageTo(_driver).base.save_image_to_allure()